package boardgamer.yavalath.client.lobby;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.RectF;
import boardgamer.common.client.Button;
import boardgamer.common.client.layout.LayoutGroup;
import boardgamer.yavalath.client.YavalathPlayer;
import boardgamer.yavalath.common.YavalathPlayerState;
import com.google.common.base.Predicate;

/**
 * @author garysoed
 */
public class LocalPlayerButton implements Button {

  private final PlayerColorPicker colorPicker;
  private final PlayerModePicker modePicker;
  private final PlayerNamePicker namePicker;
  private final LayoutGroup group;

  public LocalPlayerButton(
      int playerId,
      Predicate<YavalathPlayerState.PlayerColor> playerColorPredicate,
      Predicate<YavalathPlayerState.PlayerMode> playerModePredicate,
      Context context) {
    this(playerId, playerColorPredicate, playerModePredicate, YavalathPlayerState.PlayerMode.HUMAN, context);
  }
  
  public LocalPlayerButton(
      int playerId,
      Predicate<YavalathPlayerState.PlayerColor> playerColorPredicate,
      Predicate<YavalathPlayerState.PlayerMode> playerModePredicate,
      YavalathPlayerState.PlayerMode initialMode,
      Context context) {
    this.colorPicker = new PlayerColorPicker(playerId, playerColorPredicate);
    this.namePicker = new PlayerNamePicker(playerId, Color.RED, false, context);
    this.modePicker = new PlayerModePicker(Color.RED, initialMode, playerModePredicate,
        namePicker);
    this.group = new LayoutGroup()
        .add(colorPicker).atCurrentRect()
        .add(modePicker).relativeToEdge(colorPicker, LayoutGroup.Direction.RIGHT, 0)
        .add(namePicker).relativeToEdge(modePicker, LayoutGroup.Direction.RIGHT, 0);
  }
  
  public YavalathPlayer getPlayer() {
    return new YavalathPlayer(namePicker.getPlayerName(), colorPicker.getPlayerColor());
  }
  
  public YavalathPlayerState getPlayerState() {
    return new YavalathPlayerState(colorPicker.getPlayerColor(), modePicker.getPlayerMode());
  } 

  public YavalathPlayerState.PlayerMode getPlayerMode() {
    return modePicker.getPlayerMode();
  }

  public void refreshPlayerName() {
    namePicker.refreshPlayerName();
  }

  @Override
  public void draw(Canvas canvas) {
    colorPicker.draw(canvas);
    modePicker.draw(canvas);
    namePicker.draw(canvas);
  }

  @Override
  public void setBoundingRect(RectF boundingRect) {
    group.setBoundingRect(boundingRect);
  }

  @Override
  public RectF getBoundingRect() {
    return group.getBoundingRect();
  }

  @Override
  public void handleEvent(float eventX, float eventY, int eventAction) {
    colorPicker.handleEvent(eventX, eventY, eventAction);
    modePicker.handleEvent(eventX, eventY, eventAction);
    namePicker.handleEvent(eventX, eventY, eventAction);
  }

  @Override
  public RectF getCollisionBox() {
    return getBoundingRect();
  }
}
